package com.dems.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dems.dao.dataobject.CfgAlarmStrategy;
import com.dems.dao.dataobject.User;
import com.dems.dao.mapper.la.CfgAlarmStrategyMapper;
import com.dems.domain.AlarmStrategyReqVO;
import com.dems.service.CfgAlarmStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CfgAlarmStrategyServiceImpl implements CfgAlarmStrategyService {

    @Autowired
    private CfgAlarmStrategyMapper alarmStrategyMapper;

    @Override
    public int create(CfgAlarmStrategy alarmStrategy, User user) {
        alarmStrategy.setCreateBy(user.getName());
        alarmStrategy.setCreateTime(new Date());
        alarmStrategy.setCreateUserId(user.getUserId());
        return alarmStrategyMapper.insert(alarmStrategy);
    }

    @Override
    public int update(CfgAlarmStrategy alarmStrategy, User user) {
        alarmStrategy.setUpdateBy(user.getName());
        alarmStrategy.setUpdateTime(new Date());
        alarmStrategy.setUpdateUserId(user.getUserId());
        if(alarmStrategy.getIsDelete() != null && alarmStrategy.getIsDelete().intValue() == 1){
            return alarmStrategyMapper.deleteByPrimaryKey(alarmStrategy.getId());
        }
        return alarmStrategyMapper.updateByPrimaryKeySelective(alarmStrategy);
    }

    @Override
    public int delete(CfgAlarmStrategy alarmStrategy, User user) {
        return alarmStrategyMapper.deleteByPrimaryKey(alarmStrategy.getId());
    }

    @Override
    public Page queryAlarmStrategyByPage(AlarmStrategyReqVO alarmStrategyReqVO) {
        Page<CfgAlarmStrategy> page = new Page<>(alarmStrategyReqVO.getPageNum(), alarmStrategyReqVO.getPageSize());
        CfgAlarmStrategy alarmStrategy = new CfgAlarmStrategy();
        BeanUtils.copyProperties(alarmStrategyReqVO, alarmStrategy);
        return alarmStrategyMapper.queryByPage(page, alarmStrategy);
    }
}
